<?php
namespace Seller\Controller;
use Think\Controller;
class ExpressController extends CommonController {

    public function _initialize() {
        $params = array("addexpress","del","install","setarea","addarea","editarea","delarea","query");
        parent::_initialize($params);
        $this->assign("now_module" ,"express");
    }

    //快递列表
    public function index() {
        $count = M('express')->count();
        $page = set_page($count, C('PAGE_NUM'));
        $show = $page->show();
        $express = M('express')->where(array("shop_id"=>session("shop_id")))->order('id DESC')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('express',$express);
        $this->assign('page',$show);
        $this->display();
    }

    //添加快递
    public function addExpress()
    {
        if (IS_POST) {
            $data = array(
                'e_name' => I('post.c_name'),
                'info' => I('post.info'),
                'shop_id'=>session("shop_id")
            );
            $res = M("express")->where(array("e_name"=>I("post.c_name"),"shop_id"=>session("shop_id")))->find();
            if(!empty($res)){
                $this->error("已存在同名的快递",$_SERVER['HTTP_REFERER']);
            }
            M('express')->add($data);
            $this->success("操作成功" ,U("express/index") ,1);
        } else {
            $this->display();
        }
    }

    //卸载快递
    public function del() {
        $id = I('post.id');
        $bool = M('express')->where( array('id'=>$id) )->save( array('state'=>-1) );
        $this->ajaxReturn($bool);
    }

    //安装软件
    public function install() {
        $id = I('post.id');
        $bool = M('express')->where( array('id' => $id) )->save( array('state' => 1) );
        $this->ajaxReturn($bool);
    }

    //设置区域列表
    public function setArea() {
        $id = I('get.id');
        $count = D('ExpressArea')->relation(true)->where( array('express_id'=>$id) )->count();
        $page = set_page($count, C('PAGE_NUM'));
        $show = $page->show();
        $expressArea = D('ExpressArea')->relation(true)->where( array('express_id'=>$id) )->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('expressArea',$expressArea);
        $this->assign('page',$show);
        $this->assign('id', $id);
        $this->display();
    }

    //添加配送区域
    public function addArea()
    {
        if (IS_POST) {
//            快递id
            $id = I('post.id');
            $data = array(
                'express_id' => $id,
                'ea_name' => I('post.ea_name'),
                'standard' => I('post.standard'),
                'up_num' => I('post.up_num'),
                'down_num'=> I('post.down_num')
            );
            $areas = I('post.areas');
//            去逗号
            $area = substr($areas,0,strlen($areas)-1);
            $arrArr = explode(',', $area);
            foreach ($arrArr as $v) {
                $res = M('expressAreaname')->where(array('area_name' => $v ,'express_id'=>$id))->find();
                if ($res) {
                    $this->error("已存在有此地区邮费设置");
                }
            }
            $bool = M('expressArea')->add($data);
            foreach ($arrArr as $v) {
                $data = array(
                    'express_area_id' => $bool,
                    'area_name' => $v,
                    'express_id' => $id
                );
                    M('expressAreaname')->add($data);
            }
            $this->success("操作成功" ,U('Express/setArea', array("id" =>$id)) ,1);
        } else {
            $this->assign('id', I('get.id'));
            $this->display();
        }
    }

    //编辑配送区域
    public function editArea()
    {
        $id = I('get.id');
        if (IS_POST) {
            $express_id = I('post.express_id');
            $areas = I('post.areas');
            $area = substr($areas ,0 ,strlen($areas)-1);
            $arrArr = explode(',', $area);
            foreach ($arrArr as $v) {
                $res = M('expressAreaname')->where(array('area_name' => $v,'express_id'=>$express_id))->find();
                if($res){
                    if ($res['express_area_id'] != I('post.id')) {
                        $this->error("已存在有此地区邮费设置");
                    }
                }
            }
            $data = array(
                'ea_name' => I('post.ea_name'),
                'standard' => I('post.standard'),
                'up_num' => I('post.up_num'),
                'down_num' => I('post.down_num')
            );
            //修改数据
            M('expressArea')->where( array('id'=>I('post.id')) )->save($data);
            //先把以前的数据删除
            M('expressAreaname')->where( array('express_area_id' => I('post.id')) )->delete();
            //重新插入数据
            foreach ($arrArr as $v) {
                $data = array(
                    'express_area_id' => I('post.id'),
                    'area_name' => $v,
                    'express_id' => $express_id
                );
                M('expressAreaname')->add($data);
            }
            $this->success("操作成功" ,U('Express/setArea', array("id" =>$express_id)) ,1);
        } else {
            $expressArea = D('ExpressArea')->relation(true)->where( array('id'=>$id) )->find();
            $this->assign('expressArea', $expressArea);
            $this->assign('id', $id);
            $this->display();
        }

    }

    //删除配送区域
    public function delArea() {
        $id = I('post.id');
        $bool = D('ExpressArea')->relation(true)->delete($id);
        $this->ajaxReturn($bool);
    }

    //查询快递
    public function query() {
        $order_id = trim(I('post.order_id'));
        $express = new \Org\Util\Express();
        $res = $express->getorder($order_id);
        if ($res['message'] == 'ok') {
            $this->ajaxReturn($res['data']);
        } else {
            $this->ajaxReturn(-1);
        }
    }

}